/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rehearser;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.Integer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiEvent;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Track;

/**
 *
 * @author Martin Gumucio
 */
public class testBed {

    String song = "Sista_krigsradet_melodi_och_komp.MID";
    Sequencer player;
    Sequence currentSound;
    static InputStreamReader converter = new InputStreamReader(System.in);
    static BufferedReader in = new BufferedReader(converter);

    public static void main(String[] args) {
        try {
            testBed t = new testBed();
            if (args.length > 0){ t.playSimple(args[0]);
            } else { t.playSimple("");}
            
            
        } catch (MidiUnavailableException ex) {
            Logger.getLogger(testBed.class.getName()).log(Level.SEVERE, null, ex);

        }
    }

    public void playSimple(String inputSong) throws MidiUnavailableException {
        {
            if (!inputSong.isEmpty()) song = inputSong;
            try {
                File midiFile = new File(song);

                System.out.println("Rehearser:");
                System.out.println("Now player: " + midiFile.getName());
                System.out.println("Rehearser:");


                Sequencer sequencer = MidiSystem.getSequencer();
                sequencer.setSequence(MidiSystem.getSequence(midiFile));
                sequencer.open();
                sequencer.start();


//                sequencer.setTrackMute(0, true);
//                sequencer.setTrackMute(1, true);
//                sequencer.setTrackMute(2, true);
//                sequencer.setTrackMute(3, true);



                while (true) {
                    int inputKey;
                    if (sequencer.isRunning()) {
                        inputKey = getInt();
                        switch (inputKey) {
                            case 1:
                                sequencer.setTrackMute(1, !sequencer.getTrackMute(1));
                            case 2: // TODO -- code for muting/unmuting track 2
                            default: // do nothing
                            }

                        try {
                            Thread.sleep(1000); // Check every second
                        } catch (InterruptedException ignore) {
                            break;
                        }
                    } else {
                        break;
                    }
                }


                // Close the MidiDevice & free resources
                sequencer.stop();
                sequencer.close();
            } catch (MidiUnavailableException mue) {
                System.out.println("Midi device unavailable!");
            } catch (InvalidMidiDataException imde) {
                System.out.println("Invalid Midi data!");
            } catch (IOException ioe) {
                System.out.println("I/O Error! " + ioe.getMessage());
            }
        }
    }
    // Read a String from standard system input
    public static String getString() {
        try {
            int result = in.read(); 
            return "" + result;
        } catch (Exception e) {
            System.out.println("getString() exception, returning empty string");
            return "";
        }
    }

    // Read a char from standard system input
    public static char getChar() {
        String s = getString();
        if (s.length() >= 1) {
            return s.charAt(0);
        } else {
            return '\n';
        }
    }

    public static int getInt() {
        String s = getString();
        return Integer.valueOf(s).intValue();
    }

    public void run() throws MidiUnavailableException {
        try {
            Sequencer players = MidiSystem.getSequencer();
            players.open();
            Sequence seq = new Sequence(Sequence.PPQ, 4);
            Track track = seq.createTrack();

            ShortMessage a = new ShortMessage();
            a.setMessage(144, 1, 44, 100);
            MidiEvent noteOn = new MidiEvent(a, 1);
            track.add(noteOn);

            ShortMessage b = new ShortMessage();
            b.setMessage(126, 1, 44, 100);
            MidiEvent noteOff = new MidiEvent(b, 1);
            track.add(noteOff);

            players.setSequence(seq);
            players.start();


        } catch (InvalidMidiDataException ex) {
            Logger.getLogger(testBed.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(ex.getMessage());
        }
    }
}

